package binary_search;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-01-20
 * Time:14:38
 * Text：34. 在排序数组中查找元素的第一个和最后一个位置
 */
public class Text2 {
    public static int[] searchRange(int[] nums, int target) {
        int left=0,right=nums.length-1;
        int[] arr=new int[2];
        arr[0]=-1;
        arr[1]=-1;
        if(nums.length==0){
            return arr;
        }
        //判断左端点
        while(left<right){
            int mid=left+(right-left)/2;
            if(nums[mid]<target)left=mid+1;
            else {right=mid; }
        }
        if(nums[left]!=target){
            return arr;
        }else arr[0]=right;

        left=right;right=nums.length-1;
        while(left<right){
            int mid=left+(right-left+1)/2;
            if(nums[mid]>target)right=mid-1;
            else left=mid;
        }
        arr[1]=left;
        return arr;

    }

    public static void main(String[] args) {
        int[] array={5,7,7,8,8,10};
        int num=8;
        int[] array1=searchRange(array,num);
        System.out.println();
    }
}
